package fileReader.demo.reader;

import java.io.DataInputStream;
import java.io.IOException;


/**
 * This class is resposible for reading all the signsound specific data from files
 */
public class PointZReader extends LayerReader{

	private static boolean stupidType = false;	//this is a quick bad fix for differences among maps

	/**
	 * Method reads signsound specific data from main file (.shp)
	 * For more information read ESRI Technical Dscription 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param fileLength length of file to read
	 * @author Mari
	 */
	public void readPointZData(DataInputStream dataIn, String layer, MapLayer mapLayer, int fileLength)
	{
		try {
			int i = 100;									// mainheader on jo luettu
			while(i<fileLength)
			{
				dataIn.readInt();				//rec header
				int contLength = 2*(dataIn.readInt());		//rec header
				i = i + 8;
				
				readLittleInteger(dataIn.readInt());	//shapetype
				double x = readLittleDouble(dataIn.readDouble());		// x cordinate
				double y = readLittleDouble(dataIn.readDouble());		// y cordinate
				dataIn.skipBytes(8);		// z value
				//dataIn.skipBytes(8);		// MEASURE This value is reported in ESRI technical description but can't be found
				
				if(layer.contains("signsound")){
					createSignSound(x, y, mapLayer);
				}
				i = i + contLength;
			}
		} 
		catch (IOException e) 
		{
			readDataError();
		}
		
	}
	
	private void createSignSound(double x, double y, MapLayer mapLayer){
		PointDrawer signsound = new PointDrawer();
		signsound.setX(x);
		signsound.setY(y);
		mapLayer.data.add(signsound);
	}
	
	/**
	 * Method reads signsound specific data from database file (.dbf)
	 * For more information read dBase Language Reference 111902 and ESRI Study Report
	 * 
	 * @param dataIn data input stream for reading a file
	 * @param layer map layer in which data is stored
	 * @param numOfRecords number of records to read from a file
	 * @author Mari
	 */
	public void readSignSoundAttributes(DataInputStream dataIn, PointMapLayer layer, int numOfRecords){
		try{
			for(int n=0; n<numOfRecords; n++)
			{
				char value = (char)dataIn.readByte(); //record deleted flag
				if(value == 42)
				{
					recordDeleted();
				}	
				
				skipLayerBytes(dataIn);
				
				dataIn.skipBytes(17);	// SURVEYOID
				
				if(stupidType)
				{
					dataIn.skipBytes(8);	// DATEAC
				}
				
				dataIn.skipBytes(11);	// DEPICSOU
				
				if(stupidType)
				{
					dataIn.skipBytes(64);	// SURVNAME
				}
				
				dataIn.skipBytes(11);	// TYPESOUND
				
				if(stupidType)
				{
					dataIn.skipBytes(64);	// VERDAT
					dataIn.skipBytes(64);	// SURVNAME
					dataIn.skipBytes(11);	// SOUNDTECH
				}
				
				String depthString = "";	//DEPTH
	
				for(int m=0; m<16; m++)
				{
					depthString = depthString + (char)dataIn.readByte();
				}
						
				double depth = Double.parseDouble(depthString);
				((PointDrawer) layer.data.get(n)).setDepth(depth);
				
			}
		}catch (IOException e) {
			readDataError();
		}
	}
	
	/**
	 * @param stupidType The stupidType to set.
	 */
	public static void setStupidType(boolean stupidType) {
		PointZReader.stupidType = stupidType;
	}
}
